<script setup>
import { ref } from 'vue';
import { useUserStore } from '@/stores';
import { onMounted } from 'vue';
import { useUpdataInfo } from '@/api/user.js'
import { ElMessage } from 'element-plus';
const btnLoading = ref(false)
const userStore = useUserStore()
const formRef = ref()
const formData = ref({
    username: '',
    nickname: '',
    email: ''
})
const formRules = {
    nickname: [
        {
            required: true,
            message: '请输入用户昵称',
            trigger: 'blur'
        },
        {
            pattern: /^\S{2,10}$/,
            message: '昵称必须是2-10位的非空字符串',
            trigger: 'blur'
        }
    ],
    email: [
        {
            required: true,
            message: '请输入用户昵称',
            trigger: 'blur'
        },
        {
            pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
            message: '邮箱格式不正确',
            trigger: 'blur'
        }
    ]
}
onMounted(() => {
    formData.value = userStore.user
})
const submit = async () => {
    btnLoading.value = true
    await formRef.value.validate()
    await useUpdataInfo(formData.value)
    ElMessage.success('修改成功')
    btnLoading.value = false
    userStore.getUser()


}

</script>

<template>
    <PageContainer title="基本资料">
        <!-- 内容 -->
        <el-form class="formSection" :model="formData" ref="formRef" :rules="formRules">
            <el-form-item label="登录名称">
                <el-input placeholder="ddenlu" v-model="formData.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="用户昵称" prop="nickname">
                <el-input placeholder="请输入用昵称" v-model="formData.nickname"></el-input>
            </el-form-item>
            <el-form-item label="用户邮箱" prop="email">
                <el-input placeholder="请输入邮箱" v-model="formData.email"></el-input>
            </el-form-item>
            <el-form-item class="submit-btn-wrapper">
                <el-button type="primary" class="submit-btn" :loading="btnLoading" @click="submit">提交修改</el-button>
            </el-form-item>
        </el-form>
    </PageContainer>
</template>
<style lang="scss" scoped>
.formSection {
    width: 50%;
    padding: 0 20px;

    /* 统一对齐所有表单项 */
    :deep(.el-form-item) {
        display: flex;
        align-items: center;
        margin-bottom: 25px;


        /* 标签宽度固定 */
        .el-form-item__label {
            width: 100px;
            text-align: right;
            padding-right: 12px;
        }

        .el-input__wrapper {
            padding: 7px 11px;
            width: 100%;
        }
    }

    :deep(.submit-btn-wrapper) {
        .el-form-item__label {
            display: none;
        }

        .el-form-item__content {
            margin-left: 100px !important;
            flex: 1;
        }
    }

    .submit-btn {
        // width: 100%;
        padding: 10px;

    }
}
</style>